package controller;

import dao.PhotoDao;
import model.Photo;
import model.Users;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;

import java.sql.SQLException;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.text.SimpleDateFormat;


public class PhotowallController extends HttpServlet{
    public PhotowallController(){
        super();
    }


    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        Users user = (Users)request.getSession().getAttribute("users");
        if(user == null){
            request.setAttribute("error","please login");
            request.getRequestDispatcher("/photoWall.jsp").forward(request,response);
        }
        else {
            String describe = (String) request.getParameter("describe");

           Photo q = new Photo(describe);
            PhotoDao qdao = new PhotoDao();
            if(describe==null){
                request.setAttribute("error","please input your describe");
                request.getRequestDispatcher("/photoWall.jsp").forward(request,response);
            }
            else{
                try{
                    int i = qdao.save(q);
                    if(i==1){
                        response.sendRedirect(request.getContextPath()+"/photoWall.jsp");
                    }
                    else{
                        request.setAttribute("error","failed");
                        request.getRequestDispatcher("/photoWall.jsp").forward(request,response);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
